#include "GnCustomiseLog4cplus.h"
using namespace log4cplus;

//#define ErrorCheck(className, logger) \


inline void GetAppenName(log4cplus::tstring& appendName, log4cplus::Logger &logger)
{
	log4cplus::tchar num[2] = {0,};		
#ifdef UNICODE
	swprintf_s( num, sizeof(num), "%d", logger.getAllAppenders().size() );
#else // UNICODE
	sprintf_s( num, sizeof(num), "%d", logger.getAllAppenders().size() );
#endif // UNICODE
	appendName += num;
}

extern "C" LOG4CPLUS_EXPORT log4cplus::Appender* AppendLogerFile(eAppenderType appendType,
	log4cplus::tstring& logerName, log4cplus::tstring& filename,
	long maxFileSize, int maxBackupIndex, bool immediateFlush)
{
	log4cplus::Logger logger = log4cplus::Logger::getInstance(logerName);


	log4cplus::tstring appendName;
	GetAppenName(appendName, logger);

	log4cplus::helpers::SharedObjectPtr<log4cplus::Appender>
		appendLog(new log4cplus::RollingFileAppender(filename, maxFileSize,
		maxBackupIndex, immediateFlush));
	appendLog->setName(appendName);
	logger.addAppender(appendLog);
	return appendLog.get();
}

extern "C" LOG4CPLUS_EXPORT log4cplus::Appender* AppendLogerOutputDebug(eAppenderType appendType,
	const log4cplus::tstring& logerName)
{
	log4cplus::Logger logger = log4cplus::Logger::getInstance(logerName);
	

	log4cplus::tstring appendName;
	GetAppenName(appendName, logger);

	log4cplus::helpers::SharedObjectPtr<log4cplus::Appender>
		appendLog(new log4cplus::Win32DebugAppender());
	appendLog->setName(appendName);
	logger.addAppender(appendLog);
	return appendLog.get();	
}

extern "C" LOG4CPLUS_EXPORT void WriteLog(const log4cplus::tstring logerName,
	log4cplus::tstring message, int logLevel)
{
	Logger logger = Logger::getInstance(logerName);
	do
	{
		if( logger.isEnabledFor(logLevel) )
			logger.forcedLog(logLevel, message.c_str(), __FILE__, __LINE__);
	}
	while (0);
}

extern "C" LOG4CPLUS_EXPORT void WriteFromLoggerLog(Logger* logger,
	log4cplus::tstring message, int logLevel)
{
	do
	{
		if( logger->isEnabledFor(logLevel) )
			logger->forcedLog(logLevel, message.c_str(), __FILE__, __LINE__);
	}
	while (0);
}